﻿using System;
using OpenQA.Selenium.Appium.Service;

namespace SimulationCenter.Scripts
{
    /// <summary>
    /// 检索设备中所有关注的公众号
    /// </summary>
    public class FetchSubscriptionsScriptExecutor : ScriptExecutorBase
    {
        public event EventHandler<FetchSubscriptionArgs> OnFetchSubscription;

        private readonly int _scTaskId;

        public FetchSubscriptionsScriptExecutor(AppiumLocalService localService, int scTaskId, string serial) 
            : base(localService, serial)
        {
            _scTaskId = scTaskId;
        }

        protected override void Start()
        {
            SwitchToContactsTab();

            MoveToSubscriptionMenu();

            EnsureCurrentSubscriptionsPage();

            ScrollSubscriptions(() =>
            {
                var elements = Driver.FindElements(Const.SubscriptionTitleId);

                foreach (var item in elements)
                {
                    OnFetchSubscription?.Invoke(this, new FetchSubscriptionArgs(_scTaskId, Serial, item.Text));
                }
            });
        }
    }

    public class FetchSubscriptionArgs : EventArgs
    {
        public int ScTaskId { get; set; }

        public string Device { get; set; }

        public string Subscription { get; set; }

        public FetchSubscriptionArgs(int scTaskId, string device, string subscription)
        {
            ScTaskId = scTaskId;
            Device = device;
            Subscription = subscription;
        }
    }
}
